//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#if !defined(_LOADMISC_H_)
#define _LOADMISC_H_

#ifdef __cplusplus
extern "C" {
#endif

void Load_Main(void);

unsigned char ReadByte(void);
void WriteByte(unsigned char c);

void mcopy(unsigned long dst, unsigned long src, unsigned long size);
void ReadBlock(unsigned char *buffer, long size);
unsigned long ReadWord(void);
void WriteWord(unsigned long x);
int ReadByteWithTimeout(void);
unsigned long ReadWordNoWait(void);
void debug(unsigned char c);
void debugw(unsigned long x);
void mdump(unsigned long addr, int size);
void Delay(unsigned long msec);
void dputc(unsigned char c);
char dgetc();
char* dgets(char *s);
void dputs(const char *s);

#define queryflag   '?'
#define startflag   '<'
#define endflag     '>'
#define noteflag    '!'
#define debugflag   'x'
#define writeflag   'w'
#define eraseflag   'e'

//========================================================
//===copy from ntimage.h===================================

#define IMAGE_DOS_SIGNATURE                 0x5A4D      /* MZ */
#define IMAGE_OS2_SIGNATURE                 0x454E      /* NE */
#define IMAGE_OS2_SIGNATURE_LE              0x454C      /* LE */
#define IMAGE_NT_SIGNATURE                  0x00004550  /* PE00 */

typedef struct _IMAGE_DOS_HEADER {      /* DOS .EXE header */
    unsigned short e_magic;                     /* Magic number */
    unsigned short e_cblp;                      /* Bytes on last page of file */
    unsigned short e_cp;                        /* Pages in file */
    unsigned short e_crlc;                      /* Relocations */
    unsigned short e_cparhdr;                   /* Size of header in paragraphs */
    unsigned short e_minalloc;                  /* Minimum extra paragraphs needed */
    unsigned short e_maxalloc;                  /* Maximum extra paragraphs needed */
    unsigned short e_ss;                        /* Initial (relative) SS value */
    unsigned short e_sp;                        /* Initial SP value */
    unsigned short e_csum;                      /* Checksum */
    unsigned short e_ip;                        /* Initial IP value */
    unsigned short e_cs;                        /* Initial (relative) CS value */
    unsigned short e_lfarlc;                    /* File address of relocation table */
    unsigned short e_ovno;                      /* Overlay number */
    unsigned short e_res[4];                    /* Reserved words */
    unsigned short e_oemid;                     /* OEM identifier (for e_oeminfo) */
    unsigned short e_oeminfo;                   /* OEM information; e_oemid specific */
    unsigned short e_res2[10];                  /* Reserved words */
    long   e_lfanew;                    /* File address of new exe header */
  } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;

/*
 * File header format.
 */

typedef struct _IMAGE_FILE_HEADER {
    unsigned short  Machine;
    unsigned short  NumberOfSections;
    unsigned long   TimeDateStamp;
    unsigned long   PointerToSymbolTable;
    unsigned long   NumberOfSymbols;
    unsigned short  SizeOfOptionalHeader;
    unsigned short  Characteristics;
} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;

/*
 * Directory format.
 */

typedef struct _IMAGE_DATA_DIRECTORY {
    unsigned long   VirtualAddress;
    unsigned long   Size;
} IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES    16

/*
 * Optional header format.
 */

typedef struct _IMAGE_OPTIONAL_HEADER {
    /*
     * Standard fields.
     */

    unsigned short  Magic;
    unsigned char   MajorLinkerVersion;
    unsigned char   MinorLinkerVersion;
    unsigned long   SizeOfCode;
    unsigned long   SizeOfInitializedData;
    unsigned long   SizeOfUninitializedData;
    unsigned long   AddressOfEntryPoint;
    unsigned long   BaseOfCode;
    unsigned long   BaseOfData;

    /*
     * NT additional fields.
     */

    unsigned long   ImageBase;
    unsigned long   SectionAlignment;
    unsigned long   FileAlignment;
    unsigned short  MajorOperatingSystemVersion;
    unsigned short  MinorOperatingSystemVersion;
    unsigned short  MajorImageVersion;
    unsigned short  MinorImageVersion;
    unsigned short  MajorSubsystemVersion;
    unsigned short  MinorSubsystemVersion;
    unsigned long   Reserved1;
    unsigned long   SizeOfImage;
    unsigned long   SizeOfHeaders;
    unsigned long   CheckSum;
    unsigned short  Subsystem;
    unsigned short  DllCharacteristics;
    unsigned long   SizeOfStackReserve;
    unsigned long   SizeOfStackCommit;
    unsigned long   SizeOfHeapReserve;
    unsigned long   SizeOfHeapCommit;
    unsigned long   LoaderFlags;
    unsigned long   NumberOfRvaAndSizes;
    IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
} IMAGE_OPTIONAL_HEADER, *PIMAGE_OPTIONAL_HEADER;

#define IMAGE_SIZEOF_STD_OPTIONAL_HEADER      28
#define IMAGE_SIZEOF_NT_OPTIONAL_HEADER      224

typedef struct _IMAGE_NT_HEADERS {
    unsigned long Signature;
    IMAGE_FILE_HEADER FileHeader;
    IMAGE_OPTIONAL_HEADER OptionalHeader;
} IMAGE_NT_HEADERS, *PIMAGE_NT_HEADERS;

//=========================================================
#ifdef __cplusplus
}
#endif

#endif // _LOADMISC_H_
